/**
 *    Copyright (C) 2008  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_TOOLS_TYPES_ALGO_MINMAX_HPP
#define	_CUBEFS_CORE_TOOLS_TYPES_ALGO_MINMAX_HPP

#include <cubefs/core/tools/types/typelist.hpp>
#include <cubefs/core/tools/types/traits.hpp>
#include <cubefs/core/tools/types/algo/for_each.hpp>
#include <cubefs/api/types.hpp>

#include <cubefs/core/tools/types/algo/select.hpp>
#include <cubefs/core/tools/types/traits.hpp>

#include <cubefs/core/tools/assert.hpp>


#include <typeinfo>

namespace cubefs    {
namespace tools     {


template <
        typename List,
        template <typename Item> class ValueProvider,
        typename ValueType = BigInt
>
class MinElement {

    CUBEFS_STATIC_ASSERT(IsList<List>::Value);

    template <typename Config, typename Item, typename Accumulator>
    struct Handler {
        static const ValueType Value    = ValueProvider<Item>::Value;
        
        static const bool IsMin         = Value <= Accumulator::MinValue;

        static const ValueType MinValue = IsMin ? Value : Accumulator::MinValue;

        typedef typename IfThenElse<
                    IsMin,
                    Item,
                    typename Accumulator::MinElement
        >::Result                                                               MinElement;
    };

    struct Init {
        typedef NullType            MinElement;
        static const ValueType      MinValue = ValueTraits<ValueType>::Max;
    };

public:

    typedef typename ForEach<
                NullType,
                List,
                Handler,
                Init
    >::Result                                                                   Result0;

    typedef typename Result0::MinElement                                        Result;
};




template <
        typename List,
        template <typename Item> class ValueProvider,
        typename ValueType = BigInt
>
class MaxElement {

    CUBEFS_STATIC_ASSERT(IsList<List>::Value);

    template <typename Config, typename Item, typename Accumulator>
    struct Handler {
        static const ValueType Value    = ValueProvider<Item>::Value;

        static const bool IsMax         = Value >= Accumulator::MaxValue;

        static const ValueType MaxValue = IsMax ? Value : Accumulator::MaxValue;

        typedef typename IfThenElse<
                    IsMax,
                    Item,
                    typename Accumulator::MaxElement
        >::Result                                                               MaxElement;
    };

    struct Init {
        typedef NullType            MaxElement;
        static const ValueType      MaxValue = ValueTraits<ValueType>::Min;
    };

    typedef typename ForEach<
                NullType,
                List,
                Handler,
                Init
    >::Result                                                                   Result0;

public:

    typedef typename Result0::MaxElement                                        Result;
};

template <typename Item>
struct TypeSizeValueProvider {
    static const Int Value = ValueTraits<Item>::Size;
};



}
}

#endif  //_CUBEFS_CORE_TOOLS_TYPES_ALGO_MINMAX_HPP